package com.xsomnus.data_structures.searching;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author @xsomnus666_xiawenye★
 * @since 2019/7/18 0018 17:31
 * - 才需学也,学需静也/非淡泊无以明志，非宁静无以致远
 * <desc>
 * 字典树
 * </desc>
 */
public class Trie {

    private Node root;

    private int size;

    private class Node {
        private boolean isWord;
        private Map<Character, Node> next;

        public Node(boolean isWord) {
            this.isWord = isWord;
            next = new TreeMap<>();
        }

        Node() {
            this.isWord = false;
        }
    }


    public Trie() {
        root = new Node();
        size = 0;
    }

    public void add(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c) == null) {
                cur.next.put(c, new Node());
            }
            cur = cur.next.get(c);
        }

        if (!cur.isWord) {
            cur.isWord = true;
            size++;
        }
    }
}
